/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var threeSumClosest = function(nums, target) {
    let cur = 0
    let min = Number.MAX_SAFE_INTEGER
    let ans;
    nums.sort((a, b) =>  a -b)
    while(cur < nums.length - 2) {
        if(cur > 0 && nums[cur] === nums[cur - 1]) {
            cur++
            continue
        }
        let left = cur + 1, right = nums.length - 1
        while(left < right) {
            let sum = nums[cur] + nums[left] + nums[right]
            if(Math.abs(sum - target) < min) {
                min = Math.abs(sum - target)
                ans = sum
            }
            if(sum > target) {
                right--
            }
            else if(sum < target){
                left++
            } else return sum
        }
        cur++
    }
    return ans
};
console.log(3 >>> 1);
console.log(threeSumClosest([-1,2,1,-4], 1));